package com.fleet.project01.utils;

import android.os.Looper;
import android.widget.Toast;

import com.fleet.project01.App;


public class ToastUtils {
    private static Toast sToast;

    /**
     * 显示短时间Toast
     */
    public static void showShort(String message) {
        showToast(message, Toast.LENGTH_SHORT);
    }

    /**
     * 显示长时间Toast
     */
    public static void showLong(String message) {
        showToast(message, Toast.LENGTH_LONG);
    }

    private static void showToast(final String message, final int duration) {
        if (Looper.myLooper() == Looper.getMainLooper()) {
            show(message, duration);
        }
    }

    private static void show(String message, int duration) {
        if (sToast == null) {
            sToast = Toast.makeText(App.getInstance(), message, duration);
        } else {
            sToast.setText(message);
            sToast.setDuration(duration);
        }
        sToast.show();
    }

    /**
     * 取消Toast显示
     */
    public static void cancel() {
        if (sToast != null) {
            sToast.cancel();
        }
    }
}
